package com.china08.yxyapi.api.manage.tec.teaching.zujuan.zujuanlist;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.mg.user.YxyTeacherCenter;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkUserCenterRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 组卷列表---保存用户的操作记录(记录到学段,学科,版本,册别)
 * 
 */
@CrossOrigin
@RestController
public class ZujuanListSaveInfoController extends YxyApiBaseController {

    @Autowired
    private HomeworkUserCenterRepository homeworkUserCenterRepository;

    @Autowired
    private YxyResBookRepository bookRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/zujuanlist/saveinfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel) {
        String bookId = reqModel.bookId;
        String bookContent = reqModel.bookContent;
        String bookContentName = reqModel.bookContentName;

        YxyResBook book = bookRepository.findById(bookId);
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        String phaseId = book.getPhaseId();
        String phaseName = book.getPhaseName();
        String subjectId = book.getSubjectId();
        String subjectName = book.getSubjectName();
        String versionId = book.getVersionId();
        String versionName = book.getVersionName();
        // String cebieId = book.getSemersterId();
        String cebieName = book.getSemersterName();

        YxyTeacherCenter center = homeworkUserCenterRepository.findByTeacherIdAndStageAndSubject(userId, phaseId,
                subjectId);
        if (center == null) {
            center = new YxyTeacherCenter();
        }
        center.setTeacherId(userId);
        center.setStage(phaseId);
        center.setStageName(phaseName);
        center.setSubject(subjectId);
        center.setSubjectName(subjectName);
        center.setVersion(versionId);
        center.setVersionName(versionName);
        center.setBookId(bookId);
        center.setBookName(cebieName);
        center.setBookContent(bookContent);
        center.setBookContentName(bookContentName);
        homeworkUserCenterRepository.save(center);
        RespModel respModel = new RespModel();
        respModel.setMessage("保存用户操作成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 书id
        public String bookId;
        // 目录id
        public String bookContent;
        // 目录名称
        public String bookContentName;
    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
